<template>
	<om-page title="联系人">
			<uni-card :titleUnderline="false" :is-full="true" style="border: none;border-radius: 1px;" titleBgColor="#eee"
			 :title="'联系人(' + order + ')'">
				<uni-list>
					<uni-list-item>
						<view class="tui-flex">
							<view class="tui-col-5">
								<om-label label="称谓" required></om-label>
							</view>
							<view class="tui-right tui-col-7">
								<picker mode="selector" @change="bindAppellationChange" :range="appellationList" :value="appellationIndex" range-key="dicname">
									<view>{{ (appellationList.length && ~appellationIndex) ? appellationList[appellationIndex].dicname : '请选择' }}</view>
								</picker>
							</view>
						</view>
					</uni-list-item>
					<uni-list-item :showArrow="false">
						<view class="tui-flex">
							<view class="tui-col-5">
								<om-label label="姓名" required></om-label>
							</view>
							<view class="tui-col-7">
								<input v-model="formdata.nameF" placeholder="请输入"/>
							</view>
						</view>
					</uni-list-item>
					<uni-list-item :showArrow="false">
						<view class="tui-flex">
							<view class="tui-col-5">
								<om-label label="手机号" required></om-label>
							</view>
							<view class="tui-col-7">
								<input v-model="formdata.mobileF" placeholder="请输入"/>
							</view>
						</view>
					</uni-list-item>	
					<uni-list-item :showArrow="false">
						<view class="tui-flex">
							<view class="tui-col-5">
								<om-label label="工作单位"></om-label>
							</view>
							<view class="tui-col-7">
								<input v-model="formdata.companyF" placeholder="请输入"/>
							</view>
						</view>
					</uni-list-item>
				</uni-list>
			</uni-card>
		<view class="padding">
			<button class="bg-green margin-bottom" @click="save" >确认</button>
			<button v-if="index != null" @click="del" class="bg-grey">删除</button>
		</view>
	</om-page>
</template>

<script>
	const form = require("../../../static/js/formValidation.js")
	export default {
		data() {
			return {
				cachename:'',
				formdata:{
					appellationFCode:"",
					appellationFName:'',
					nameF:'',
					companyF:'',
					mobileF:'',
					checkF:true,//紧急联系人标识
				},
				appellationIndex:0,
				appellationList:[],//学历
				index:null,
				datas:[],
				order:0,
			}
		},
		methods: {
			save(){
				let rules = [{
					name: "appellationFName",
					rule: ["required"],
					msg: ["请输入联系人称谓"]
				},{
					name: "nameF",
					rule: ["required"],
					msg: ["请输入联系人名称"]
				},{
					name: "mobileF",
					rule: ["required","isMobile"],
					msg: ["请输入联系人手机号","请输入正确的手机号"]
				}]
				let checkRes = form.validation(this.formdata, rules);
				if (!checkRes) {
					if(this.index != null){
						Object.assign(this.datas[this.index],this.formdata)
					}else{
						this.datas.push(this.formdata)
					}
					uni.setStorage({
					  key: this.cachename,
					  data: this.datas
					})
					uni.navigateBack()
				} else {
					this.om.toast(checkRes);
				}
			},
			del(){
				if(this.index != null){
					this.datas.splice(this.index,1)
				}
				uni.setStorage({
				  key: this.cachename,
				  data: this.datas
				})
				uni.navigateBack()
			},
			bindAppellationChange(e){
				if(e){
					this.appellationIndex = e.detail.value;
				}
				this.formdata.appellationFCode= this.appellationList[this.appellationIndex].code
				this.formdata.appellationFName = this.appellationList[this.appellationIndex].dicname
			},
			initLoad(){
				let _that = this
				//称谓
				_that.om.request('dictZero/getListByCode.do', {
					code: '23597'
				}).then(res => {
					//if (res instanceof Array) {
					if(Object.prototype.toString.call(res)==='[object Array]'){
						_that.appellationList = res
						if(_that.formdata.appellationFCode){
							for(var i=0;i<_that.appellationList.length;i++) {
								var item = _that.appellationList[i]
								if(item.code == _that.formdata.appellationFCode){
									_that.appellationIndex = i
									break;
								}
							}
						}
						_that.bindAppellationChange()
					}
				});
				
			}
		},
		onLoad(e){
			let index = e.index
			let _that = this
			if(e.cachename){
				this.cachename = e.cachename
				uni.getStorage({
					key: this.cachename,
					success: function(res) {
						if(res.data && res.data.length){
							_that.datas = res.data
							if(index != null && index != undefined){
								var d = res.data[index]
								Object.assign(_that.formdata,d)
								_that.order = parseInt(index)+ 1
								_that.index = parseInt(index)
							}else{
								_that.order = res.data.length + 1
							}
							
						}else{
							_that.order = 1
						}
						_that.initLoad()
					},
					fail() {
						_that.order = 1
						_that.initLoad()
					}
				});
			}else{
				uni.navigateBack({
					
				})
			}
			
			
		}
	}
</script>

<style>

</style>
